package com.example.websocket.utils;

import com.example.websocket.pojo.ws.ResultMessage;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * @ author 韩
 * time: 2024/2/12 18:16
 */

public class MessageUtils {
    public static String getMessage(String fromName,String message,Long time) {
        try {
            ResultMessage result = new ResultMessage();
            result.setMessage(message);
            if (fromName != null) {
                result.setFromName(fromName);
            }
            result.setTime(time);
            ObjectMapper mapper = new ObjectMapper();
            return mapper.writeValueAsString(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getMessage(String fromName,String toName,String message,Long time) {
        try {
            ResultMessage result = new ResultMessage();
            result.setMessage(message);
            if (fromName != null) {
                result.setFromName(fromName);
            }
            result.setToName(toName);
            result.setTime(time);
            ObjectMapper mapper = new ObjectMapper();
            return mapper.writeValueAsString(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    public static String getGroupMessage(String fromName,String groupId,String message,Long time) {
        try {
            ResultMessage result = new ResultMessage();
            result.setMessage(message);
            if (fromName != null) {
                result.setFromName(fromName);
            }
            result.setGroupId(groupId);
            result.setTime(time);
            ObjectMapper mapper = new ObjectMapper();
            return mapper.writeValueAsString(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

